package com.hwq.sql.tool.utils;

import java.nio.charset.StandardCharsets;

public class StrUtil {

    /**
     * 判断字符串是否为无效字符串，无效返回真，有效返回假
     * 有效字符串：不为 null，去两端空格后不为空字符串
     * @param str 需要验证的字符串
     */
    public static boolean isBlank(String str) {
        if (str == null) return true;
        for (int i = 0; i < str.length(); i++) {
            char c = str.charAt(i);
            if (c != ' ') return false;
        }
        return true;
    }

    /**
     * 判断字符串为有效字符串
     * @param str 需要验证的字符串
     */
    public static boolean isNotBlank(String str) {
        return !isBlank(str);
    }

    /**
     * 替换无效字符串为有效的默认值
     * @param old 待判定的原字符串
     * @param str 调用时给定的默认字符串
     */
    public static String reInvalid(String old, String str) {
        return isBlank(old) ? str : old;
    }

    /**
     * 转化 普通字符串 为 十六进制（Hex）字符串
     * @param str 普通字符串
     */
    public static String strToHex(String str) {
        char[] chars = "0123456789ABCDEF".toCharArray();
        StringBuilder sb = new StringBuilder();
        byte[] bs;
        bs = str.getBytes(StandardCharsets.UTF_8);
        int bit;
        for (byte b : bs) {
            bit = (b & 0x0f0) >> 4;
            sb.append(chars[bit]);
            bit = b & 0x0f;
            sb.append(chars[bit]);
        }
        return sb.toString().trim();
    }

    /**
     * 解析 十六进制（Hex）字符串 为 普通字符串
     * @param hex 十六进制字符串
     */
    public static String hexToStr(String hex) {
        String str = "0123456789ABCDEF";
        char[] hexs = hex.toCharArray();
        byte[] bytes = new byte[hex.length() / 2];
        int n;
        for (int i = 0; i < bytes.length; i++) {
            n = str.indexOf(hexs[2 * i]) * 16;
            n += str.indexOf(hexs[2 * i + 1]);
            bytes[i] = (byte) (n & 0xff);
        }
        return new String(bytes, StandardCharsets.UTF_8);
    }

    /**
     * 将 某字符分割的字符串 转化为 驼峰式的字符串
     * 如：cutToHump("AA_BB_CC", '_', true) -> AaBbCc
     * 如：cutToHump("Aa-BB-cc", '-', false) -> aaBbCc
     * @param oldStr    原字符串
     * @param separator 分隔符
     * @param firstBig  结果是否首字母大写
     */
    public static String cutToHump(String oldStr, char separator, boolean firstBig) {
        StringBuilder newStr = new StringBuilder(10);
        boolean isSeparator = false;
        for (int i = 0; i < oldStr.length(); i++) {
            char c = oldStr.charAt(i);
            if (c == separator) {
                isSeparator = true;
            } else if ((i == 0 && firstBig) || isSeparator) {
                newStr.append(Character.toUpperCase(c));
                isSeparator = false;
            } else {
                newStr.append(Character.toLowerCase(c));
            }
        }
        return newStr.toString();
    }

    /**
     * 将 驼峰式的字符串 转化为 某字符分割的字符串
     * @param oldStr    原字符串
     * @param separator 分隔符
     * @param big       true 结果为纯大写、fasle 纯小写
     */
    public static String humpToCut(String oldStr, char separator, boolean big) {
        StringBuilder newStr = new StringBuilder(10);
        for (int i = 0; i < oldStr.length(); i++) {
            char c = oldStr.charAt(i);
            if (i != 0 && Character.isUpperCase(c)) {
                newStr.append(separator);
            }
            newStr.append(big ? Character.toUpperCase(c) : Character.toLowerCase(c));
        }
        return newStr.toString();
    }

}
